package org.lex.game.sudoku.model.visitor;

import java.util.HashSet;
import java.util.Set;

import ms.sudoku.util.IntSet;

import org.lex.game.sudoku.model.Point;
import org.lex.game.sudoku.model.SudokuModel;

import common.Sudokus;

public final class DuplicatesNumberVisitor implements ModelVisitor<Set<Point>> {
	private final boolean findAllDuplicates;
	private IntSet all = new IntSet();
	private Set<Point> duplicates = new HashSet<>();

	public DuplicatesNumberVisitor(boolean findAllDuplicates) {
		this.findAllDuplicates = findAllDuplicates;
	}

	@Override
	public VisitResult visit(SudokuModel model, int row, int col, int num) {
		if (Sudokus.notEmpty(num)) {
			if (this.all.contains(num)) {
				this.duplicates.add(new Point(row, col));
				if (false == findAllDuplicates) {
					return VisitResult.TERMINATE;
				}
			} else {
				this.all.add(num);
			}
		}
		return VisitResult.CONTINUE;
	}

	@Override
	public Set<Point> finish() {
		return this.duplicates;
	}
}